package org.jtb.flickrportlet;

import org.jtb.quickr.AuthResponse;
import org.jtb.quickr.AuthResponse.Token;
import org.jtb.quickr.AuthService;
import org.jtb.quickr.FrobResponse;
import org.jtb.quickr.Permissions;
import org.jtb.quickr.QuickrException;
import java.io.IOException;
import java.net.URL;

public class AuthHandler {
    private Token token = null;
    private FrobResponse frob;
    private String userName;
    private String userId;
    
    public boolean isAuthenticated() {
        if (getToken() == null) {
            return false;
        }
        return true;
    }
    
    public String getAuthUrl() throws QuickrException {
        AuthService as = new AuthService();
        frob = as.getFrob();
        URL authUrl = as.getAuthUrl(frob, Permissions.DELETE);
        
        return authUrl.toString();
    }
        
    public void authenticate() throws QuickrException {
        AuthService as = new AuthService();
        AuthResponse ares = as.getToken(frob);
        token = ares.getAuthToken();
        userName = ares.getAuthUser().getUserName();
        userId = ares.getAuthUser().getNsId();
    }

    public Token getToken() {
        return token;
    }
    
    public void clearToken() {
        this.token = null;
    }
    
    public String getUserName() {
        return userName;
    }

    public String getUserId() {
        return userId;
    }
}
